'use strict';

const { Service } = require('ee-core');
const Services = require('ee-core/services');
const Log = require('ee-core/log');
const HttpClient = require('ee-core/httpclient');
const { app, BrowserWindow, session } = require('electron')
const CoreWindow = require('ee-core/electron/window');
const Addon = require('ee-core/addon');


/**
 * 示例服务（service层为单例）
 * @class
 */
class WmxService extends Service {

  constructor(ctx) {
    super(ctx);
  }

  /**
   * test
   */
  async test(args) {
    let obj = {
      status:'ok',
      params: args
    }

    Log.info('WmxService obj:', obj);


    return obj;
  }
  
  
  /**
   * 店铺基础信息
   */
  async arfq(args) {
    
      console.log("arfq:"+JSON.stringify(args))

     
      let contentUrl   = 'https://air.alibaba.com/apps/rax-app/rfq-seller/detail.html?rfqId=ID1Y_pGBWEy5HobPuPgNNyTQBceJdPX5oewYWNl7HGK34slPlKnKMLZN6TPXdnrR6L_'
    
      let opt = {
        title: "windowTitle",
        center: true, // 居中打开
        webPreferences:{
          devTools: false, // 禁用开发者工具
          menu: null, // 禁用菜单
          nodeIntegration:false
        }
      }
      const win = Addon.get('window').create("windowName", opt);
  
      // load page
      win.loadURL(contentUrl, { userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" });
      

      win.webContents.session.webRequest.onBeforeRequest((details, callback) => {
          // 在这里添加你的请求拦截逻辑
          // 例如，如果请求的 URL 包含特定字符串，可以取消该请求：
          if (details.url.includes('mtop.alibaba.rfq.detail.get.h5')) {
            console.log("rfq url :"+details.url);
            callback({ cancel: true })
          } else {
            callback({})
          }
        })
   


      return "";

    
  }

  async sellerProfileAjax(args) {
  

    var alicookie =await  Services.get('storage').getTestDataSqlite('cookie');
    var token_csrf =await  Services.get('storage').getTestDataSqlite('token_csrf');


    console.log("token_csrf:"+token_csrf.cookie)

    const url = "https://crmweb.alibaba.com/rightcenter/right/supplierIdentity.json";
    const options = {
      method: 'POST',
      dataType: 'json',
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
        'Cookie': alicookie.cookie,
        'Origin': 'https://crmweb.alibaba.com',
        'Referer': 'https://crmweb.alibaba.com/rightcenter/right/verifiedRightCenter.htm',
      },
      data: {
        _csrf_token: token_csrf.cookie,
      },
      timeout: 15000,  
    };
    const hc = new HttpClient();
    const response = await hc.request(url, options);

    return response.data.content;
  }

  async marketing(args) {
  

    var alicookie =await  Services.get('storage').getTestDataSqlite('cookie');
    var token_csrf =await  Services.get('storage').getTestDataSqlite('token_csrf');


    console.log("marketing token_csrf:"+token_csrf.cookie)

    const url = "https://www2.alibaba.com/api/maCard/marketing";
    const options = {
      method: 'POST',
      dataType: 'json',
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
        'Cookie': alicookie.cookie,
        'Origin': 'https://i.alibaba.com',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      },
      data: {
        ads: '{"productLineId":110101}',
        type: 'normal',
        data: '{}',
        _csrf: token_csrf.cookie
      },
      timeout: 15000,  
    };
    const hc = new HttpClient();
    const response = await hc.request(url, options);


    return response.data;
  }


}

WmxService.toString = () => '[class WmxService]';
module.exports = WmxService;